package com.trailwalker.traceability.net.client;


import com.trailwalker.traceability.entitiy.application.GlobalData;
import com.trailwalker.traceability.entitiy.body.*;
import com.trailwalker.traceability.net.client.api.DataSourceApi;
import com.trailwalker.traceability.net.result.ApiResult;
import com.trailwalker.traceability.net.rxbus.RxBus;
import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

public class RemoteDataSource extends BaseApiRemoteDataSource<DataSourceApi> {

    private static RemoteDataSource instance = null;

    public RemoteDataSource(GlobalData globalData) {
        super(globalData, DataSourceApi.class);
    }

    public static RemoteDataSource getInstance(GlobalData globalData) {
        if (instance == null) {
            synchronized (RemoteDataSource.class) {
                if (instance == null) {
                    instance = new RemoteDataSource(globalData);
                }
            }
        }
        return instance;
    }

    /**
     * 注册
     *
     * @param accountRequestBody
     * @return
     */
    public Observable<ApiResult<String>> register(AccountRequestBody accountRequestBody) {
        return getApi().register(accountRequestBody).compose(RxBus.applySchedulers());
    }

    /**
     * 登陆
     *
     * @param accountRequestBody
     * @return
     */
    public Observable<ApiResult<LoginResponseBody>> login(AccountRequestBody accountRequestBody) {
        return getApi().login(accountRequestBody).compose(RxBus.applySchedulers());
    }

    /**
     * 获取工人列表
     *
     * @return
     */
    public Observable<ApiResult<WorkerListResponseBody>> getWorkerList() {
        return getApi().getWorkerList().compose(RxBus.applySchedulers());
    }

    /**
     * 录入工序
     *
     * @param processRequestBody
     * @return
     */
    public Observable<ApiResult<ProcessResponseBody>> trace(ProcessRequestBody processRequestBody) {
        return getApi().trace(processRequestBody).compose(RxBus.applySchedulers());
    }

    /**
     * 特殊的三种工序 录入工序
     *
     * @param processRequestBody
     * @return
     */
    public Observable<ApiResult<ProcessResponseBody>> traceUpload(ProcessRequestBody processRequestBody) {
        File file = processRequestBody.getFile();
        MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        String encodeFileName = "";
        try {
            encodeFileName = URLEncoder.encode(file.getName(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        builder.addFormDataPart("filename", encodeFileName);
        builder.addFormDataPart("processName", processRequestBody.getProcessName());
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        builder.addFormDataPart("file", encodeFileName, requestBody);
        return getApi().traceUpload(builder.build()).compose(RxBus.applySchedulers());
    }

    /**
     * 工序检测
     *
     * @param processRequestBody
     * @return
     */
    public Observable<ApiResult<ProcessResponseBody>> check(ProcessRequestBody processRequestBody) {
        return getApi().check(processRequestBody).compose(RxBus.applySchedulers());
    }

}
